package com.uuah.server.message;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * TODO comment ...
 *
 * <p>
 * update record:updator，update time，update content and version
 * </p>
 *
 * @author <a href="jonny_quan@hotmail.com">jonny</a>
 * @date 2009 2009-3-27 下午01:56:23
 * @version 1.0.0
 */
public class BaseMessage implements Serializable {
	/**
	 * Serial version
	 */
	private static final long serialVersionUID = -6105691921086093748L;

	protected Object message;

	protected Map context;

	public BaseMessage(Object message) {
		this.message = message;
		context = new HashMap();
	}

	/**
	 * Converts the message implementation into a String representation
	 *
	 * @return String representation of the message payload
	 * @throws Exception
	 *             Implementation may throw an endpoint specific exception
	 */
	public String getPayloadAsString(String encoding) throws Exception {
		return message.toString();
	}

	/**
	 * Converts the message implementation into a String representation
	 *
	 * @return String representation of the message
	 * @throws Exception
	 *             Implemetation may throw an endpoint specific exception
	 */
	public byte[] getPayloadAsBytes() throws Exception {
		return getPayloadAsString(message.toString()).getBytes();
	}

	/**
	 * @return the current message
	 */
	public Object getPayload() {
		return message;
	}

	/**
	 * Adds a map of properties to associated with this message
	 *
	 * @param properties
	 *            the properties add to this message
	 */
	public void addProperties(Map properties) {
		context.putAll(properties);
	}

	/**
	 * Removes all properties on this message
	 */
	public void clearProperties() {
		context.clear();
	}

	/**
	 * Returns a map of all properties on this message
	 *
	 * @return a map of all properties on this message
	 */
	public Map getProperties() {
		return context;
	}

	public void setProperty(Object key, Object value) {
		context.put(key, value);
	}

	public Object getProperty(Object key) {
		return context.get(key);
	}

	public String toString() {
		return "BaseMessage{" + "message=" + message + ", context=" + context
				+ "}";
	}
}
